package com.ssy.lingxi.order.serviceimpl.feign;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.message.api.feign.SystemMessageControllerFeign;
import com.ssy.lingxi.message.api.vo.request.SystemMessageRequest;
import com.ssy.lingxi.order.service.feign.IMessageFeignService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 消息服务Feign接口调用实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-08-16
 */
@Service
public class MessageFeignServiceImpl implements IMessageFeignService {
    private static final Logger logger = LoggerFactory.getLogger(MessageFeignServiceImpl.class);

    @Resource
    private SystemMessageControllerFeign systemMessageControllerFeign;

    /**
     * 发送采购商、供应商的订单消息
     *
     * @param memberId    会员Id
     * @param roleId      会员角色Id
     * @param messageCode 操作编码
     * @param orderNo     订单号
     * @param digest      订单摘要
     */
    @Async
    @Override
    public void sendOrderMessageAsync(Long memberId, Long roleId, Long userId, String messageCode, String orderNo, String digest) {
        List<String> parameters = Stream.of(orderNo, digest).collect(Collectors.toList());
        SystemMessageRequest request = new SystemMessageRequest();
        request.setMemberId(memberId);
        request.setRoleId(roleId);
        request.setMessageNotice(messageCode);
        request.setParams(parameters);
        request.setUserId(userId);
        Wrapper<?> wrapper = systemMessageControllerFeign.sendSystemMessage(request);
        if (wrapper.getCode() != ResponseCode.SUCCESS.getCode()) {
            logger.error("发送订单消息错误：" + wrapper.getMessage());
        }
    }


    /**
     * 发送采购商、供应商的订单消息
     * @param memberId    会员Id
     * @param roleId      会员角色Id
     * @param messageCode 操作编码
     * @param params      消息参数列表
     */
    @Async
    @Override
    public void sendOrderMessageAsync(Long memberId, Long roleId, Long userId, String messageCode, List<String> params) {
        SystemMessageRequest request = new SystemMessageRequest();
        request.setMemberId(memberId);
        request.setRoleId(roleId);
        request.setMessageNotice(messageCode);
        request.setParams(params);
        request.setUserId(userId);
        Wrapper<?> wrapper = systemMessageControllerFeign.sendSystemMessage(request);
        if (wrapper.getCode() != ResponseCode.SUCCESS.getCode()) {
            logger.error("发送订单消息错误：" + wrapper.getMessage());
        }
    }
}
